#ifndef __t_string_h__
#define __t_string_h__

#include <string> 
#include <algorithm> 
#include <functional> 
#include <cctype>

namespace base
{



template<typename T>
class tstring
{
public:
    typedef std::basic_string<T, std::char_traits<T>, std::allocator<T> > value_type;
    typedef typename value_type::size_type size_type;

    tstring()
    {
    }

    tstring( const value_type &str )
    {
        str_ = str;
    }

    tstring( const tstring &obj )
    {
        str_ = obj.str_;
    }

    tstring & operator=( const value_type &str )
    {
        str_ = str;
        return *this;
    }

    tstring & operator=( const tstring &obj )
    {
        if( &obj != this )
        {
            str_ = obj.str_;
        }

        return *this;
    }

public:
    const T *c_str()
    {
        return str_.c_str();
    }

    void trim()
    {
        if( str_.size() > 0 )
        {
            std::basic_string<T>::iterator c;

            for( c = str_.begin(); c != str_.end() && iswspace(*c++); ); str_.erase( str_.begin(), --c );

            for( c = str_.end(); c != str_.begin() && iswspace(*--c); ); str_.erase( ++c, str_.end() );
        }
    }

    size_type tokenize( const value_type &sep, size_type pos, value_type &token )
    {
        token.clear();
        if( str_.empty() || str_.size() <= pos )
        {
            return str_.npos;
        }

        value_type s = str_.substr( pos );

        size_type p = 0;
        size_type find = 0;
        while( token.empty() )
        {
            find = s.find( sep.c_str(), p );
            if( find == s.npos )
            {
                token = s.substr( p );
                return s.npos;
            }
            else
            {
                token = s.substr( p, find - p );
                p += sep.size();
            }
        }

        return find + pos;
    }

private:
    value_type str_;
};



}



#endif